const requireDirectory = require('require-directory')
const Router = require('koa-router')
const session = require('koa-session')
const KoaRedis = require('koa-redis')
const redis = require('redis');
const RateLimit = require('koa-ratelimit');

class InitManager {
    static initCore(app) {
        //入口方法
        InitManager.app = app
        InitManager.initLoadRouters()
        InitManager.loadHttpException()
        InitManager.loadConfig()
        // InitManager.loadRedis()
    }

    static loadConfig(path = '') {
        const configPath = path || process.cwd() + '/config/config.js'
        global.config = require(configPath)
    }

    static initLoadRouters() {
        //path config
        const apiDirectory = `${process.cwd()}/app/api`
        requireDirectory(module, apiDirectory, {
            visit: whenLoadModule
        })

        function whenLoadModule(obj) {
            if (obj instanceof Router) {
                InitManager.app.use(obj.routes())
            }
        }
    }

    static loadHttpException() {
        global.errs = require('./http-exception')
    }

    static loadRedis() {
    }
}

module.exports = InitManager
